﻿/****************
 * Copyright 2016 Brian Tabone
 * Licensed under the Apache 2.0 License
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * DirectorPropertiesViewModel.cs
 * Data Model that interacts with the Director services to collect their properties for display in DirectorWeb
 * 08/21/2016
 */
using Microsoft.AspNet.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace DirectorWeb.Models
{
    public class DirectorPropertiesViewModel
    {
        public DirectorPropertiesViewModel() : base()
        {
            SelectedServiceProperties = "None";
        }

        /// <summary>
        /// The user selected service to view
        /// </summary>
        private String _selectedService;
        public String SelectedService
        {
            get
            {
                return _selectedService;
            }
            set
            {
                _selectedService = value;
            }
        }

        /// <summary>
        /// Property string received service
        /// </summary>
        public String SelectedServiceProperties
        { get; set; }

        /// <summary>
        /// Get the list of services (based on endpoints in the ConnectionEndpoints.conf file
        /// </summary>
        public SelectList Services
        {
            get
            {

                var settings = WebConfigurationManager.AppSettings;

                return new SelectList(settings, "key");
            }
        }

        /// <summary>
        /// Reach out to the service and get the properties. This method is synchronous
        /// </summary>
        public void GetProperties()
        {
            using (HttpClient client = new HttpClient())
            {
                var settings = WebConfigurationManager.AppSettings;
                client.BaseAddress = new Uri(settings[SelectedService]);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                Task<HttpResponseMessage> response = client.GetAsync("api/properties");
                response.Wait();
                if (response.Result.IsSuccessStatusCode)
                {
                    Task<string> rawBytes = response.Result.Content.ReadAsStringAsync();
                    rawBytes.Wait();
                    SelectedServiceProperties = rawBytes.Result;
                }
            }
        }

        public void SetProperty(int id, string value)
        {
            using (HttpClient client = new HttpClient())
            {
                var settings = WebConfigurationManager.AppSettings;
                client.BaseAddress = new Uri(settings[SelectedService]);
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("id",id.ToString()),
                    new KeyValuePair<string, string>("value", value) 
                });
                
                Task<HttpResponseMessage> response = client.PostAsync("/api/properties", content);
                response.Wait();
                if (response.Result.IsSuccessStatusCode)
                {
                    Task<string> rawBytes = response.Result.Content.ReadAsStringAsync();
                    rawBytes.Wait();
                }
            }
        }
    }
}
